package com.example.service;

import com.example.dao.AdminDao;
import com.example.entity.Admin;
import com.example.enums.ResultEnum;
import com.example.exception.StudentException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author John
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdminService {

    @Resource
    private AdminDao adminDao;

    public ModelAndView loginCheck(String id, String password) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("student/error");
        Admin admin = adminDao.loginCheck(id);
        if (admin == null) {
            throw new StudentException(ResultEnum.NULL_ERROR);
        } else if (admin.getPassword().equals(password)) {
            mav.setViewName("student/index");
            return mav;
        }
        return mav;
    }


    public Admin findById(String id) {
        Admin admin = adminDao.loginCheck(id);
        if (admin == null) {
            throw new StudentException(ResultEnum.NULL_ERROR);
        } else {
            return admin;
        }
    }


    public int updatepassword(String id, String password) {
        return adminDao.updatepassword(id, password);
    }
}
